package com.ufgov.ip.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ufgov.ip.dao.IuapHirerDao;
import com.ufgov.ip.dao.IuapHirerMybatisDao;
import com.ufgov.ip.entity.IpHirer;
import com.ufgov.ip.sysmanager.dao.ICompanyDao;
import com.ufgov.ip.util.CopyPropertiesUtil;

/**
 * 用户管理服务层
 * 书写readonly是说明没有特殊标识服务层只有只读事物
 * @author guangsa
 *
 */
@Service
//@Transactional(readOnly=true)
public class HirerAccountService {

	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	private static final int SALT_SIZE = 8;

	private static Logger logger = LoggerFactory.getLogger(UserAccountService.class);

	@Autowired
	private IuapHirerDao hirerDao;
	
	@Autowired
	private ICompanyDao coDao;

	public void setHirerDao(IuapHirerDao hirerDao) {
		this.hirerDao = hirerDao;
	}
	
	public void setCoDao(ICompanyDao coDao) {
		this.coDao = coDao;
	}



	/**
	 * 根据租户名称得到相应的用户信息
	 * @param loginName
	 * @return
	 */
	public IpHirer findHirerByLoginName(String loginName) {
		return hirerDao.findByHirerName(loginName);
	}
	
	/**
	 * 根据租户电话得到相应的用户信息
	 * @param loginName
	 * @return
	 */
	public IpHirer findHirerByCellphoneNo(String cellphoneNo) {
		return hirerDao.findHirerByCellphoneNo(cellphoneNo);
	}
	
	/**
	 * 根据租户邮箱得到相应的用户信息
	 * @param loginName
	 * @return
	 */
	public IpHirer findHirerByEmail(String email) {
		return hirerDao.findHirerByEmail(email);
	}
	
	public IpHirer findHirerByEmailOrLoginNameOrCellphoneNo(String loginName){
		return hirerDao.findHirerByEmailOrLoginNameOrCellphoneNo(loginName);
	}
	
	public IpHirer findHirerByPassword(String pwd){
		return hirerDao.findHirerByPassword(pwd);
	}

	public IpHirer findHirerByHirerId(String hirerId) {
		return hirerDao.findHirerByHirerId(hirerId);
	}
	
	@Autowired
	IuapHirerMybatisDao iuapHirerMybatisDao;
	
	public IpHirer findHirerByHirerIdMybatis(String hirerId) {
		IpHirer ipHirer= iuapHirerMybatisDao.findHirerByHirerId(hirerId);
		return ipHirer;
	}
	
	@Transactional
	public boolean saveHirerInfo(IpHirer ipHirer) {

		try {
			//IpHirer findByHirerName = hirerDao.findByHirerName(ipHirer.getHirerName());
			//ipHirer.setHirerId(findByHirerName.getHirerId());
			IpHirer findHirerByHirerId = hirerDao.findHirerByHirerId(ipHirer.getHirerId());
			CopyPropertiesUtil.setProperty(findHirerByHirerId);
			CopyPropertiesUtil.copyProperty(findHirerByHirerId, ipHirer);
			hirerDao.save(findHirerByHirerId);
			String hirerId = ipHirer.getHirerId();
			coDao.updateCompanyByHirerIdAndLevelNum(hirerId,1,ipHirer.getHirerShortName());
		} catch (Exception e) {
			logger.error("注册用户失败!"+e);
			try {
				throw new Exception();
			} catch (Exception e1) {
               
				e1.printStackTrace();
			}
		}
		return true;
		
		
		
	}
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void updateHirerByHirerId(String hirerId, String path) {
           
		hirerDao.updateHirerByHirerId(hirerId,path);
		
	}

	public List<IpHirer> findAll() {
		// TODO 自动生成的方法存根
		return hirerDao.findHirerAll();
	}
	
	
	
	
}
